package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.NewsOrderItemBinding;
import sz.szsmk.citizencard.dialog.ItemDelDialog;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.NoticeMainres;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.Utils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/13 15:23
 * description:
 */
public class NewsOrderAdapter extends BaseRecyclerAdapter<NewsOrderAdapter.NewsOrderAdapterViewHodler> {
    private Context mContext;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    /**
     * 用于记录需要删除的view的position
     */
    private ArrayList<Integer> removeList = new ArrayList<Integer>();
    private static ReleaseCallBack mCallBack;
    private static DelitemCallBack mDelItemCallBack;
    private static UpitemCallBack mUpItemCallBack;

    private LinearLayout noticeLL;
    private TextView mDragView;
    private List<NoticeMainres.MainPageInfoListBean> itemData;
    private NoticeMainres.MainPageInfoListBean mainPageInfoListBean;


    public NewsOrderAdapter(Context context) {
        mContext = context;

    }

    public void setItemData(List<NoticeMainres.MainPageInfoListBean> itemData) {
        this.itemData = itemData;
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDeleteBtnCilck(View view, int position);
    }

    @Override
    public int getItemCount() {
        return null == itemData ? 0 : itemData.size();
    }

    @Override
    public NewsOrderAdapterViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        return NewsOrderAdapterViewHodler.creat(parent);
    }
    @Override
    public void onBindViewHolder(NewsOrderAdapterViewHodler holder, int position) {
        loadItemData(position, holder.binding);
    }
    private void loadItemData(final int position, NewsOrderItemBinding binding) {
        if (itemData != null) {
            binding.newsItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    final ItemDelDialog itemDelDialog = new ItemDelDialog(mContext, "删除");
                    itemDelDialog.show();
                    itemDelDialog.setOnOkClickListener(new ItemDelDialog.OnOkClickListener() {
                        @Override
                        public void onOkClick() {
                            itemDelDialog.cancel();
                            if (mDelItemCallBack != null) {
                                mDelItemCallBack.onDelItemCallBack(position);
                            }
                        }
                    });
                    itemDelDialog.setmOnZDingClickListener(new ItemDelDialog.OnZDingClickListener() {
                        @Override
                        public void onZDingClick() {
                            itemDelDialog.cancel();
                            if (mUpItemCallBack != null) {
                                mUpItemCallBack.onUpItemCallBack(position);
                            }
                        }
                    });
                    return true;
                }
            });
            mainPageInfoListBean = itemData.get(position);
            int unReadNumber = mainPageInfoListBean.getUnReadNumber();
            mDragView = (TextView) binding.getRoot().findViewById(R.id.mDragView);
            if (removeList.contains(position)) {
                mDragView.setVisibility(View.GONE);
            } else {
                if (unReadNumber == 0) {
                    mDragView.setVisibility(View.GONE);
                } else {
                    mDragView.setVisibility(View.VISIBLE);
                }
                if (unReadNumber > 99) {
                    mDragView.setText("99+");
                } else {
                    mDragView.setText(String.valueOf(unReadNumber));
                }
            }
            noticeLL = (LinearLayout) binding.getRoot().findViewById(R.id.notice_ll);
            noticeLL.getLayoutParams().width = Utils.getScreenWidth(mContext);
            String typeName = mainPageInfoListBean.getTypeName();
            L.e(Constant.TAG, "--------" + typeName + position);
            binding.newsItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    NoticeMainres.MainPageInfoListBean listBean = itemData.get(position);
                    String messageType = listBean.getMessageType();
                    String typeName1 = itemData.get(position).getTypeName();
                    bundle.putString("title", typeName1);
                    bundle.putString("messageType", messageType);
                    JumpUtils.jumptoNatvie(mContext, "noticelist", 0, bundle);
                }
            });
            binding.newsOrderItemTv1.setText(mainPageInfoListBean.getTypeName());
            binding.newsOrderItemTv2.setText(mainPageInfoListBean.getLastMessageContent());
            binding.newsTime.setText(mainPageInfoListBean.getLastInfoDate());
        }
        Glide.with(mContext)
                .load(mainPageInfoListBean.getImgUrl())
                .error(R.drawable.zhanweitu)
                .centerCrop()
                .override(100, 100)
                .transform(new GlideRoundTransform(mContext))
                .into(binding.newsIcon);


    }

    public void removeData(int position) {
        itemData.remove(position);
//        删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }


    public void setPopReleaseCallBack(ReleaseCallBack callBack) {
        mCallBack = callBack;
    }

    public void setDelItemCallBack(DelitemCallBack callBack) {
        mDelItemCallBack = callBack;
    }

    public void setUpItemCallBack(UpitemCallBack callBack) {
        mUpItemCallBack = callBack;
    }

    public interface ReleaseCallBack {
        void onPopReleaseCallBack(String messageType);
    }

    public interface DelitemCallBack {
        void onDelItemCallBack(int position);
    }

    public interface UpitemCallBack {
        void onUpItemCallBack(int position);
    }

    public static class NewsOrderAdapterViewHodler extends BaseHolder {

        private NewsOrderItemBinding binding;

        public static NewsOrderAdapterViewHodler creat(ViewGroup parent) {
            NewsOrderItemBinding binding = NewsOrderItemBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new NewsOrderAdapterViewHodler(binding);
        }

        public NewsOrderAdapterViewHodler(NewsOrderItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
